﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using DigitalQuran.Extensions;
using DigitalQuran.Data;
using DigitalQuran.Collection;

namespace DigitalQuran
{
    /// <summary>
    /// Scope of Text
    /// </summary>
    public enum TextScope
    {
        /// <summary>
        /// Entire Book
        /// </summary>
        Book,

        /// <summary>
        /// Selected Content Only
        /// </summary>
        Selection,

        /// <summary>
        /// Highlighted Text Only
        /// </summary>
        HighlightedText
    };
    
    /// <summary>
    /// Represent Digital Qur'an User. This class has only one instance.
    /// </summary>
    public class Client
    {
        /// <summary>
        /// Default Loaded Tafseer
        /// </summary>
        public const string DefaultTafseer = "en.qarai";

        /// <summary>
        /// Default Loaded Translation
        /// </summary>
        public const string DefaultTranslation = "en.pickthall";

        /// <summary>
        /// Default loaded Transliteration
        /// </summary>
        public const string DefaultTransliteration = "en.transliteration";

        /// <summary>
        /// Default Recitation
        /// </summary>
        public const string DefaultRecitation = "Alafasy_64kbps";        

        private Client()
        {                        
            TextModes = new List<string>();

            Statistics = new Statistic.StatisticalEngine();
            Simplifier = new Text.SimplificationEngine();

            Translations = new TranslationTable();
            Recitations = new RecitationTable();

            Numerology = new Numerology.NumerologyEngine();

            Bookmarks = new BookmarkCollection();
            BrowseHistory = new BrowseHistoryCollection();
            SearchHistory = new SearchHistoryCollection();

            Selection = new Selection(QuranObjectType.Chapter, new List<int> { 0 });
        }

        private void LoadData()
        {
            Simplifier.LoadRules();
            Numerology.LoadSystems();

            ActiveTextMode = Globals.DefaultTextMode;
        }

        /// <summary>
        /// Gets the User Name of currently logged on to the OS
        /// </summary>
        public string UserName { get { return Environment.UserName; } }

        /// <summary>
        /// Gets or sets User Selection
        /// </summary>
        public Selection Selection { get; set; }

        /// <summary>
        /// Gets Numerology Systems
        /// </summary>
        public Numerology.NumerologyEngine Numerology { get; private set; }

        /// <summary>
        /// Gets Simplifier responsible for text simplification
        /// </summary>
        public Text.SimplificationEngine Simplifier { get; private set; }

        /// <summary>
        /// Gets the Statistics Engine for advance statistics
        /// </summary>
        public Statistic.StatisticalEngine Statistics { get; private set; }

        /// <summary>
        /// Gets all loaded translations
        /// </summary>
        public TranslationTable Translations { get; private set; }

        /// <summary>
        /// Gets Recitation Table
        /// </summary>
        public RecitationTable Recitations { get; private set; }

        /// <summary>
        /// Gets the TextScope for current client
        /// </summary>
        public TextScope TextScope { get; private set; }        

        /// <summary>
        /// Gets all loaded TextModes
        /// </summary>
        public List<string> TextModes { get; private set; }

        string textMode = "";
        /// <summary>
        /// Gets or Set the Active text mode
        /// </summary>
        public string ActiveTextMode
        {
            get { return textMode; }
            set
            {
                if (textMode != value)
                {
                    textMode = value;

                    Collection.WordCollection.LoadTextMode(value);                    
                    VerseCollection.UpdateText(value);
                }
            }
        }

        /// <summary>
        /// Gets the collection of Bookmarks for this client
        /// </summary>
        public BookmarkCollection Bookmarks { get; private set; }

        /// <summary>
        /// Gets browse history collection
        /// </summary>
        public BrowseHistoryCollection BrowseHistory { get; private set; }

        /// <summary>
        /// Gets search history collection
        /// </summary>
        public SearchHistoryCollection SearchHistory { get; private set; }


        static Client _Instance = null;
        /// <summary>
        /// Gets singlton instance of clinet
        /// </summary>
        public static Client Instance
        {
            get
            {
                if (_Instance == null)
                    CreateInstance();

                return _Instance;
            }
        }

        /// <summary>
        /// Create new instance of client
        /// </summary>
        /// <returns></returns>
        public static Client CreateInstance()
        {
            if (_Instance == null)
            {
                Book.CreateInstance();

                _Instance = new Client();
                _Instance.LoadData();
            }

            return Instance;
        }
    }
}